package com.lovo.controller;


import com.lovo.model.DOS.StudentDO;
import com.lovo.model.DTO.StudentDTO;
import com.lovo.result.ResponseResult;
import com.lovo.service.IStudentService;
import com.lovo.util.RedisUtil;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bdk
 * @since 2020-11-24
 */
@Api("学生信息的控制器")
@RestController
@RequestMapping("/students")
public class StudentController {
    @Resource
    private IStudentService studentService;
    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("查询所有学生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页" ,example = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示数" ,example = "5")
    })
    @GetMapping
    @ResponseResult
    public Object showAll( Integer page, Integer size){
        return studentService.selectAll(page,size);
    }
//
    @ApiOperation("增加学生")
    @PostMapping
    @ResponseResult
    public Object save(@RequestBody @Valid StudentDTO studentDTO){
        studentService.saveStudent(studentDTO);
        return null;
    }

    @ApiOperation("删除学生")
    @DeleteMapping("/{id}")
    @ResponseResult
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生id" ,example = "23"),
    })
    public Object del(Long id){
        boolean b = studentService.removeById(id);
        redisUtil.delall();
        if (b) return 1;
        return 0;
    }
    @ApiOperation("更新学生")
    @PutMapping("/{id}")
    @ResponseResult
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生id" ,example = "23"),
    })
    public Object update(Long id, @RequestBody StudentDTO studentDTO){
        StudentDO studentDO = new StudentDO(id);
        BeanUtils.copyProperties(studentDTO,studentDO);
        studentDO.setStuID(id);

        studentService.updateById(studentDO);
        redisUtil.delall();
        return null;


    }

}

